package com.huatai.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.util.BeanUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.manage.mapper.TbTrialInfoMapper;
import com.huatai.manage.entity.TbTrialInfoEntity;
import com.huatai.manage.query.PageQuery;
import com.huatai.manage.service.TbTrialInfoService;
import com.huatai.manage.vo.ResultVo;
import com.huatai.manage.vo.TbTrialInfoVo;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * (TbTrialInfo)表服务实现类
 *
 * @author luoxc
 * @since 2023-03-14 14:13:39
 */
@Service
public class TbTrialInfoServiceImpl extends ServiceImpl<TbTrialInfoMapper, TbTrialInfoEntity> implements TbTrialInfoService {

	/**
	 * 根据试验id获取信息
	 *
	 * @param trialId
	 * @return
	 */
	@Override
	public TbTrialInfoEntity getByTrialId(String trialId) {
		return getOne(Wrappers.<TbTrialInfoEntity>lambdaQuery().eq(TbTrialInfoEntity::getTrialId, trialId));
	}

	/**
	 * 分页获取试验基础信息列表
	 *
	 * @param pageQuery
	 * @return
	 */
	@Override
	public Page<TbTrialInfoEntity> pageList(PageQuery<TbTrialInfoEntity> pageQuery) {
		Page<TbTrialInfoEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		TbTrialInfoEntity data = pageQuery.getData();
		String trialType = data.getTrialType();
		String trialName = data.getTrialName();
		LambdaQueryWrapper<TbTrialInfoEntity> wrapper = Wrappers.<TbTrialInfoEntity>lambdaQuery()
			.eq(StringUtil.isNotBlank(trialType), TbTrialInfoEntity::getTrialType, trialType)
			.like(StringUtil.isNotBlank(trialName), TbTrialInfoEntity::getTrialName, trialName);
		return page(page, wrapper);
	}

	/**
	 * 根据试验id集合删除试验信息
	 *
	 * @param trialIds
	 * @return
	 */
	@Override
	public boolean removes(List<String> trialIds) {
		if (trialIds.isEmpty()) return false;
		return remove(Wrappers.<TbTrialInfoEntity>lambdaQuery().in(TbTrialInfoEntity::getTrialId, trialIds));
	}

	/**
	 * 根据试验id修改信息
	 *
	 * @param tbTrialInfoEntity
	 * @return
	 */
	@Override
	public boolean update(TbTrialInfoEntity tbTrialInfoEntity) {
		LambdaUpdateWrapper<TbTrialInfoEntity> wrapper = Wrappers.<TbTrialInfoEntity>lambdaUpdate();
		wrapper.eq(TbTrialInfoEntity::getTrialId, tbTrialInfoEntity.getTrialId());
		return update(tbTrialInfoEntity, wrapper);
	}

	/**
	 * @return
	 * @Description
	 * @Date 2023/4/4
	 * @Param
	 */
	@Override
	public List<ResultVo> selectList() {
		List<ResultVo> resultVos = new ArrayList<>();
		List<TbTrialInfoEntity> list = this.list();
		if (list.isEmpty()) return null;
		list.forEach(i -> {
			resultVos.add(new ResultVo(i.getTrialId(), i.getTrialName()));
		});
		return resultVos;
	}

	/**
	 * 查询所有数据
	 *
	 * @return
	 */
	@Override
	public List<TbTrialInfoVo> trialList() {
		List<TbTrialInfoVo> trialInfoVos = new ArrayList<>();
		List<TbTrialInfoEntity> trialInfoEntities = this.list();
		trialInfoEntities.forEach(info -> {
			trialInfoVos.add(BeanUtil.copyProperties(info, TbTrialInfoVo.class));
		});
		return trialInfoVos;
	}

	/**
	 * 判断数据是否存在
	 *
	 * @param trialId
	 * @return
	 */
	@Override
	public boolean isExist(String trialId) {
		long count =
			this.count(Wrappers.<TbTrialInfoEntity>lambdaQuery().eq(TbTrialInfoEntity::getTrialId, trialId));
		return count > 0;
	}
}

